<?php

namespace app\adminapi\logic\setting\lang;

use app\common\logic\BaseLogic;
use app\common\service\ConfigService;

/**
 * 语言逻辑层
 * Class LangLogic
 * @package app\adminapi\logic\setting
 */
class ConfigLogic extends BaseLogic
{

    /**
     * 获取翻译配置
     * @return array
     */
    public static function getTranslateConfig()
    {
        $config = [
            ConfigService::get('translate', 'hs', [
                'type' => 'hs',
                'name' => self::getNameDesc(strtoupper('hs')),
                'status' => 1
            ]),
        ];
        return $config;
    }

    /**
     * 设置翻译配置
     * @param array $params
     * @return true
     */
    public static function setTranslateConfig(array $params)
    {
        $type = $params['type'];
        $params['name'] = self::getNameDesc(strtoupper($type));
        ConfigService::set('translate', $type, $params);
        $default = ConfigService::get('translate', 'engine', false);
        if ($params['status'] == 1 && $default === false) {
            // 启用当前短信配置 并 设置当前短信配置为默认
            ConfigService::set('translate', 'engine', strtoupper($type));
            return true;
        }
        if ($params['status'] == 1 && $default != strtoupper($type)) {
            // 找到默认短信配置
            $defaultConfig = ConfigService::get('translate', strtolower($default));
            // 状态置为禁用 并 更新
            $defaultConfig['status'] = 0;
            ConfigService::set('translate', strtolower($default), $defaultConfig);
            // 设置当前短信配置为默认
            ConfigService::set('translate', 'engine', strtoupper($type));
            return true;
        }
        return true;
    }

    /**
     * @notes 查看配置详情
     * @param $params
     * @return array|int|mixed|string|null
     */
    public static function detail($params)
    {
        $default = [];
        $typeName = self::getNameDesc(strtoupper($params['type']));
        switch ($params['type']) {
            case 'hs':
                $default = [
                    'access_key' => '',
                    'secret_key' => '',
                    'status' => 1,
                    'name' => $typeName,
                ];
                break;
        }
        $result = ConfigService::get('translate', $params['type'], $default);
        $result['status'] = intval($result['status'] ?? 0);
        return $result;
    }

    /**
     * @notes 获取翻译平台名称
     * @param $value
     * @return string
     */
    public static function getNameDesc($value)
    {
        $desc = [
            'HS' => '火山翻译',
        ];
        return $desc[$value] ?? '';
    }
}